package com.deamon.file.api;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping(value = "/upload")
public class UploadFileApi {

    @CrossOrigin
    @PostMapping("/uploadFile")
    public String uploadFileTest1(@RequestParam("file2") MultipartFile file2) {
        String targetFilePath = "D:\\dd";
        String fileName = UUID.randomUUID().toString().replace("-", "");
        File targetFile = new File(targetFilePath + File.separator + fileName);

        FileOutputStream fos = null;
        FileInputStream fis = null;
        try {
            fis = (FileInputStream)file2.getInputStream();
            fos = new FileOutputStream(targetFile);
            byte[] temp = new byte[1024];
            int i = fis.read(temp);
            while (i != -1) {
                fos.write(temp, 0, temp.length);
                fos.flush();
                i = fis.read(temp);
            }
        } catch (IOException e) {
        } finally {
            try {
                fis.close();
                fos.close();
            } catch (IOException e) {
            }
        }
        return "上传成功！";
    }
}
